package com.livesource.client.webservices.subversion;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.livesource.client.model.entry.ProjectFeature;

/**
 * Communicates via REST Web Service
 * 
 */
public class RequestSaveClassDescription {

	// http://livesourceagile.com:8080/LiveSourceWebServices/SaveClassDescriptionService
	// ?repositoryURL=https://tictactoe4j.googlecode.com/svn/trunk
	// &username=alline.oliveira&password=bD8Km8se2rV9
	// &filePath=src/tictactoe/authentication/client/Login.java
	// &classDescription=Form%20to%20identify%20the%20name%20and%20the%20password%20of%20the%20player.
	// &classStatus=ToDo&classType=Feature

	private static String url = "http://livesourceagile.com:8080/LiveSourceWebServices/SaveClassDescriptionService";

	public static void sendPostRequest(String projectURL, String filePath,
			String username, String password, ProjectFeature feature) {

		username = "alline.oliveira";
		password = "bD8Km8se2rV9";

		String urlSave = url + "?repositoryURL=" + projectURL + "&username="
				+ username + "&password=" + password;

		urlSave += "&filePath=" + filePath;

		String status = "";
		if (feature.getStatus() != null) {

			status = feature.getStatus();
		}

		String description = "";
		if (feature.getFeatureDescription() != null) {

			description = feature.getFeatureDescription();
		}

		String type = "";
		if (feature.getFeatureType() != null) {

			type = feature.getFeatureType();
		}

		String featureInfo = "&classDescription=" + description
				+ "&classStatus=" + status + "&classType=" + type;

		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
				urlSave + featureInfo);

		// builder.setRequestData(requestData);

		try {
			Request request = builder.sendRequest(null, new RequestCallback() {

				public void onError(Request request, Throwable exception) {
					// displayError("Couldn't retrieve JSON");
				}

				public void onResponseReceived(Request request,
						Response response) {
					if (200 == response.getStatusCode()) {
						// updateTable(asArrayOfStockData(response.getText()));
					} else {
						// displayError("Couldn't retrieve JSON ("
						// + response.getStatusText() + ")");
					}
				}
			});

		} catch (RequestException e) {
			// displayError("Couldn't retrieve JSON");
		}

	}
}